<?php
//发布者用户中心操作类

class Procenter{
     
    public function index(){
        //显示发布者表中的头像和名称
        $pro=D('promulgator');
        $prolist=$pro->field("id,username,head")->where($_SESSION['loginuser']['id'])->find();  //发布者信息数组
        $this->assign('prolist',$prolist);


        //显示消息和站内信提醒条数信息
        $mess=D('messages');

        //定义查询消息数组
        $need=array("state"=>0,"torole"=>3,"too"=>$_SESSION['loginuser']['id'],"forole"=>array(1,2,3));
        $messtotal=$mess->where($need)->total();   //与发布者相关的消息个数
		 $this->assign('messtotal',$messtotal);
		 //定义查询站内信数组
		$needs=array("state"=>0,"torole"=>3,"too"=>$_SESSION['loginuser']['id'],"forole"=>0);
        $letotal=$mess->where($needs)->total();   //与发布者相关的站内信个数
        $this->assign('letotal',$letotal);
       

        //显示发布者自己的项目
        $obj=D("project");
        $objlist=$obj->where(array("proid"=>$_SESSION['loginuser']['id']))->limit(3)->order("addtime desc")->select();  //与发布者相关的项目数组
		$this->assign('objlist',$objlist);
		//查询发布者发布的项目个数
		$prototal=$obj->where(array("proid"=>$_SESSION['loginuser']['id']))->total();
		$this->assign('prototal',$prototal);
		
		//查询发布者待评价项目个数
		$pintotal=$obj->where(array("proid"=>$_SESSION['loginuser']['id'],"prostatus"=>7))->total();
		$this->assign('pintotal',$pintotal);
		
		//查询发布者待确认项目订单个数
		$obor=D("objectorders");
		$obortotal=$obor->where(array("proid"=>$_SESSION['loginuser']['id'],"oborstatus"=>0))->total();
		$this->assign('obortotal',$obortotal);
		
		//查询发布者未处理投标订单个数
		$bidor=D("bidorders");
		$bidtotal=$bidor->where(array("proid"=>$_SESSION['loginuser']['id'],"bidorstatus"=>0))->total();
		$this->assign('bidtotal',$bidtotal);
		
        $this->display();

      //此方法向模板中输出当前发布者的姓名，头像，相关的消息条数，发布者的项目。 
    }

    //获取用户信息修改页
    public function edit(){      
        $this->assign('list',D('promulgator')->find($_SESSION['loginuser']['id']));
        $this->display("edit");
    }

    //用户信息修改
    public function update(){
        $pro=D("promulgator");
        if($pro->where($_SESSION['loginuser']['id'])->update($_POST,1,1)>0){           
             $this->success("修改成功",1,"index");
        }else{
            $this->error($pro->getMsg(),2,"procenter/edit/id/".$_SESSION['loginuser']['id']);
        }
        
        
    }

     //获取密码设置页
    public function getsetpass(){
        $this->display("getsetpass");

    }
	
	//验证原密码是否正确
    public function yanoldpass(){
        $oldpass=$_POST['oldpass'];		
		$pro=D("promulgator");
		$prooldpass=$pro->field("pass")->find($_SESSION['loginuser']['id']); 
		//P($prooldpass);
		if(md5($oldpass)==$prooldpass['pass']){  //获得的密码加密与数据库的密码进行比较
			echo "yes";
		}else{
			echo "no";
		}	
		exit();
	}
	
	//验证新密码是否正确
    public function yanpass(){
        $pass=$_POST['pass'];		
		if(preg_match("/^[A-Za-z0-9]{6,}$/i",$pass)==1){  //密码的规则到底是什么？？
			echo "yes";
		}else{
			echo "no";
		}	
		exit();
	}
	

    //执行密码修改
    public function setpass(){
         //设置一个数组只接受新密码字段
         if(D('promulgator')->where($_SESSION['loginuser']['id'])->update()>0){           
             $this->success("修改成功",1,"index");
        }else{
            $this->error("修改失败",2,"procenter/getsetpass/id/".$_SESSION['loginuser']['id']);
        }                
    }

    //验证名字是否正确
    public function yanname(){
        $name=$_POST['name'];
        if(preg_match("/^[^0-9a-zA-Z_]{4,12}$/i",$name)==1){
            echo "yes";
        }else{
            echo "no";
        }
        
    }
	
	//验证邮箱是否正确和是否唯一
    public function yanmail(){
        $mail=$_POST['mail'];
		//导出数据库的邮箱进行对比
		//P($mail);
		
		$pro=D("promulgator");
		$promail=$pro->field("email")->where(array("id !="=>$_SESSION['loginuser']['id']))->select(); //查询出以存在的邮箱
		//P($promail);
		foreach($promail as $v){
			if(in_array($mail,$v)){
					echo "邮箱已被注册";
					exit();
			}		
		}
		if(preg_match("/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/i",$mail)==1){
			echo "yes";
		}else{
			echo "no";
		}	
		exit();
	}
	
	//验证手机号码是否正确和是否唯一
    public function yanphone(){
        $phone=$_POST['phone'];
		//导出数据库的手机号码进行对比
		$pro=D("promulgator");
		$prophone=$pro->field("phone")->where(array("id !="=>$_SESSION['loginuser']['id']))->select(); //查询出以存在的邮箱
		//P($prophone);
		foreach($prophone as $v){
			if(in_array($phone,$v)){
				echo "手机号码已存在";
				exit();
			}				
		}
		if(preg_match("/^1[3-8]\d{9}$/i",$phone)==1){
					echo "yes";
		}else{
					echo "no";
		}
		
		exit();
	}
     //获取头像设置页
    public function getsethead(){

        $this->assign('list',D('promulgator')->find($_SESSION['loginuser']['id']));
        $this->display("getsethead");
    }

    //修改头像
    public function sethead(){
            $upfile = new FileUpload();  //执行brophp中的文件上传类
			
			if($upfile->upload("pic")){
			   //上传成功
                $filename = $upfile->getFileName();  //获得上传文件名字
				//等比缩放图片
				$img=new Image();
				$filename=$img->thumb($filename,300,200);
               
			   //将上传的图片更新到数据库中
			   $head['head']=$filename;
                D('promulgator')->where($_SESSION['loginuser']['id'])->update($head);  //上传头像名字				
			}else{
			   //上传失败
			   $filename="no";   //定义一个上传失败的值，随便定义
			}
			echo "<script>window.parent.dogetpic('{$filename}');</script>";  //不管上传成功还是失败都要调用             模板中的dogetpic函数，此处为调用Iframe对应的页面的函数，需使用window.parent
			exit();      
    }

    public function docut(){
			$img = new Image();
			$img->cut($_POST['name'],$_POST['x'],$_POST['y'],$_POST['w'],$_POST['h']);
			echo "ok";
	}
		
    
}
